package com.windward.vii.web.controller;

import com.windward.vii.common.Constants;
import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.Goods;
import com.windward.vii.dao.entity.Order;
import com.windward.vii.domain.order.OrderVO;
import com.windward.vii.service.GoodsService;
import com.windward.vii.service.OrderService;
import com.windward.vii.service.UserTokenService;
import com.windward.vii.utils.Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiansj on 2017/8/26.
 */
@Controller
public class OrderController {

    private static Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    UserTokenService userTokenService;
    @Resource
    OrderService orderService;
    @Resource
    GoodsService goodsService;

    // 支付类型：1微信，2支付宝，3苹果支付
    @RequestMapping(value = "v1/api1/order/create", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult order(@RequestParam Long goodsId,
                            @RequestParam Integer payType,
                            HttpServletRequest request,
                            @RequestHeader(value="token") String token){
        if(goodsId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "购买商品不存在，ID错误");
        }
        if(payType < 1 && payType > 3) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "支付类型错误，1微信、2支付宝、3苹果支付");
        }
        Goods goods = goodsService.getGoodsById(goodsId);
        if(goods == null) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "购买商品不存在");
        }

        Long uid = userTokenService.getUidByToken(token);
        String ip = Utils.getRemoteHost(request);
        if(payType == Constants.Order.PAY_TYPE_WEIXIN) {
            return orderService.createOrderForWeixinPay(uid, goods, ip);
        } else if(payType == Constants.Order.PAY_TYPE_APPLE) {
//            return orderService.createOrderForApplePay(uid, goods, ip);
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "支付类型错误");
        } else {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "支付类型错误");
        }
    }


    // 充值记录，分页加载
    @RequestMapping(value = "v1/api1/recharge/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getRechargeList(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize,
                                      @RequestHeader(value="token") String token,
                                      @RequestHeader(value="User-Agent") String userAgent) {
        logger.info("pageNum={}", pageNum);
        logger.info("pageSize={}", pageSize);
        pageNum = pageNum > 0 ? pageNum : 1;
        Long uid = userTokenService.getUidByToken(token);
        List<Order> orderList = orderService.getUserOrderPageList(uid, pageNum, pageSize);
        List<OrderVO> orderVOList = new ArrayList<>();
        if(orderList != null && orderList.size() > 0) {
            for(Order order : orderList) {
                try {
                    OrderVO orderVO = new OrderVO();
                    BeanUtils.copyProperties(order, order);
                    orderVOList.add(orderVO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(orderVOList);
        return jsonResult;
    }

}
